import { specialFocusInfo, getContractById, signWithContent } from '@/api/index';
import DZUtils from '@/utils/DZUtils';

export default {
  methods: {
    async specialFocusCheck(type) {
      const res = await specialFocusInfo(this.prodCode);
      if (res.returnCode === '0' && res.dataSet[0]) {
        const data = res.dataSet[0];
        let html = '';
        let id = '';
        // 私募、信托、小集合，特殊关注不匹配禁止购买
        // 判断 70岁
        if (type == '2') {
          if (data.fundSafeFlag == '0' || data.gtSeventy == '1') {
            let notMatchInfo = ``;
            if (data.fundSafeFlag == '0') {
              notMatchInfo += `<p>您拟购买的产品并非“保本保收益”型产品，产品的净值波动可能会导致您的本金亏损。</p>`;
            }
            if (data.gtSeventy == '1') {
              notMatchInfo += `<p>您的年龄已超过70岁。</p>`;
            }
            const { notMatchCondition } = require('@/components/contracts/agreement.js');
            html = notMatchCondition(this.userInfo, notMatchInfo);
            DZUtils.iAlert('温馨提示', html, '我知道了', () => {
              // 执行页面组件specialFocusCancel方法
              this.specialFocusCancel && this.specialFocusCancel();
            });
          } else {
            this.specialFocusCallback();
          }
        } else {
          // 其他产品
          if (data.fundSafeFlag == '0' || data.gtSixty == '1') {
            if (data.fundSafeFlag == '0') {
              const res2 = await getContractById({ type: 'focusFundSafe' });
              if (res2.returnCode === '0' && res2.dataSet[0]) {
                id = res2.dataSet[0].id;
                html += res2.dataSet[0].templateContent;
              }
            }
            if (data.gtSixty == '1') {
              const res3 = await getContractById({ type: 'focusGtsixty' });
              if (res3.returnCode === '0' && res3.dataSet[0]) {
                id = res3.dataSet[0].id;
                html += res3.dataSet[0].templateContent;
              }
            }
            html = `<p>尊敬的投资者：</p>${html}`;
            DZUtils.iConfirm('提示', html, '确认并下一步', '退出购买', () => {
              this.specialFocusMark(id, html);
            }, () => {
            });
          } else {
            this.specialFocusCallback();
          }
        }
      }
    },
    // 供父组件调用的方法，留痕
    async specialFocusMark(id, html) {
      const params = {
        id,
        content: html.replace(/<[^>]+>/g, ''),
        batchNo: this.fourFac.batchNo,
        source: 'specialFocusCheck'
      };
      const { returnCode, dataSet, returnInfo } = await signWithContent(params);
      if (returnCode === '0') {
        console.log('协议中心留痕，留痕成功');

        this.specialFocusCallback();
      } else {
        DZUtils.iDialog(returnInfo);
      }
    }
  }
};